// pages/work/index.js
let app = getApp();
var $ = require('../../utils/ajax.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    inputShowed: false,
    keywordVal: "",
    tabs: ["待办", "已办","所有"],
    workActiveIndex: 0,//
    sliderOffset:0,
    sliderLeft: 10,
    loadData: { "loadMoreHidden": true, "loadingHidden": true, 'loadMoreText': '暂无数据' },
    scroll: { "scrollTop": 0, "scrollHeight": 0,"windowHeight":0},
    params:{
      "token": '',
      "listType": 1,//页面类型，1:待审批 2:已审批 3:全部
      "limit": app.data.limit,
      "page": 1
    },
    pageTotal: 0,
    undoWorkCount:0,
    permissions:[],//用户权限
    debounce: null
  },
  tabClick: function (e) {
    let tabIndex = e.currentTarget.id;//0待处理 1已处理 2所有
    let windowHeight = this.data.scroll.windowHeight;
    let h = tabIndex != '0' ? windowHeight - 80 : windowHeight -40;
    let type = parseInt(tabIndex) + 1;
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      workActiveIndex: e.currentTarget.id,
      ['scroll.scrollHeight']: h,
      ['params.listType']: type,
      ['params.page']: 1,
      list:[]
    });
    console.log(this.data.params)
    this.doPostDataList(this,this.data.params);
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      keywordVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      keywordVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      keywordVal: e.detail.value,
      list:[],
      ['params.subject']: e.detail.value
    });
    this.doPostDataList(this,this.data.params);
  },
  //查询列表信息
  doPostDataList(_this, params) {
    _this.setData({
      ['loadData.loadingHidden']: false
    })
    $.post({
      url: getApp().data.app_server + '/weixin/workorder/list',
      data: params,
      header: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' },
    }).then((res) => {
      console.log(res);
      let data = res.data.data;
      if (res.data.count > 0) {
        var params = _this.data.params;
        var pageTotal = Math.ceil(res.data.count / _this.data.params.limit);
        _this.setData({
          list: _this.data.list.concat(data),//列表拼接
          ['loadData.loadingHidden']: true,
          params: params,
          pageTotal: pageTotal
        })
        if (pageTotal == 1) {
          _this.setData({
            ['loadData.loadMoreText']: '已经到底部了',
            ['loadData.loadMoreHidden']: false
          })
        }
      } else {
        _this.setData({
          list: _this.data.list.concat(data),//列表拼接
          ['loadData.loadMoreText']: '暂无数据',
          ['loadData.loadingHidden']: true,
          ['loadData.loadMoreHidden']: false
        })
      }

    }).catch((err) => {
      console.log(err);
    });
  },
  //查看详情
  toDetails(e) {
    console.log(e.currentTarget.id);
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: 'detail/todoDetail?id=' + id + '&workActiveIndex=' + this.data.workActiveIndex
    })
  },
  //标记重点
  markPoint(e) {
    var isMarked = e.currentTarget.dataset.ismarked;
    isMarked = isMarked == 0 ? 1 : 0;
    var that = this;
    $.post({
      url: getApp().data.app_server + '/weixin/workorder/orderKey',
      data: { token: that.data.params.token, id: e.currentTarget.id, key: isMarked}
    }).then((res) => {
      console.log(res);
      if (res.data.code == 200){
        that.setData({
          list:[]
        })
        that.doPostDataList(that,that.data.params);
      }
    }).catch((err) => {
      console.log(err);
    });
  },
  //催单
  cuidan(e){
    console.log("催单");
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: 'cuidan/index?id=' + id
    })
  },
  bindDownLoad: function () {
    var _this = this;
    let debounce = this.data.timeout;
    if (debounce) clearTimeout(debounce);
    let curPage = _this.data.curPage + 1;
    // 防抖函数
    debounce = setTimeout(function () {
      //判断是否是最后一页，最后一页不加载数据了
      if (_this.data.pageTotal <= _this.data.params.page) {
        _this.setData({
          ['loadData.loadMoreText']: '已经到底部了',
          ['loadData.loadMoreHidden']: true
        })
      } else {
        _this.setData({
          ['params.page']: curPage,
          ['loadData.loadMoreHidden']: false
        })
        _this.doPostDataList(_this, _this.data.params);
      }
    }, 500);
    _this.setData({
      debounce: debounce
    })
  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      ['scroll.scrollTop']: event.detail.scrollTop
    });
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //var permissions = wx.getStorageSync("permissions");
    this.setData({
      ['params.token']: app.data.token,
      undoWorkCount: app.data.undoWorkCount,
      permissions: app.data.permissions
    });
    this.doPostDataList(this,this.data.params);
    //这里要非常注意，微信的scroll-view必须要设置高度才能监听滚动事件，所以，需要在页面的onLoad事件中给scroll-view的高度赋值
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.info(res.windowHeight);
        that.setData({
          ['scroll.scrollHeight']: res.windowHeight - 40,
          ['scroll.windowHeight']: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      ['params.token']: app.data.token,
      undoWorkCount: app.data.undoWorkCount
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})